<?php 
/**
 * >>> ToDo <<<
 * - function copy
 */
namespace Library\IO;

/**
 * Provides static methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.
 * @namespace Library\IO
 * @author Raphael DE FREITAS (RaphyTheGeek@gmail.com)
 * @version 1.0
 * 
 */
final class Directory
{
	/**
	 * Moves a file or a directory and its contents to a new location.
	 * @param string $oldname The name of the file to move.
	 * @param string $newname The new path for the file.
	 * @return boolean TRUE on success; otherwise, FALSE.
	 */
	public static function move($oldname, $newname) {
		return rename($oldname, $newname);
	}
	
	/**
	 * Creates all directories and subdirectories in the specified path.
	 * @param string $pathname The directory path to create.
	 * @return boolean TRUE on success; otherwise, FALSE.
	 */
	public static function createDirectory($pathname) {
		return mkdir($pathname);
	}
	
	/**
	 * Deletes an empty directory from a specified path.
	 * @param string $directory The name of the directory to remove.
	 * @return RUE on success; otherwise, FALSE.
	 */
	public static function delete($path) {
		foreach(Directory::getDirectories($path) as $directory)
			Directory::delete($path.'/'.$directory);
		foreach(Directory::getFiles($path) as $file)
			File::delete($path.'/'.$file);
		return rmdir($path);
	}
	
	/**
	 * Gets the names of subdirectories in the specified directory.
	 * @param string $path The path for which an array of subdirectory names is returned.
	 * @return array An array of the full names of subdirectories in the specified path
	 */
	public static function getDirectories($path) {
		$directories = array();
		foreach(scandir($path) as $directory) {
			if(Directory::exists($path.'/'.$directory) && $directory != '.' && $directory != '..')
				array_push($directories, $directory);
		}
		return $directories;
	}
	
	/**
	 * Returns the names of files in the specified directory.
	 * @param string $path The directory from which to retrieve the files.
	 * @return array An array of the names for the files in the specified directory.
	 */
	public static function getFiles($path) {
		$files = array();
		foreach(scandir($path) as $file) {
			if(File::exists($path.'/'.$file) && $file != '.' && $file != '..')
				array_push($files, $file);
		}
		return $files;
	}
	
	/**
	 * Determines whether the given path refers to an existing directory.
	 * @param string $filename The path to test.
	 * @return boolean TRUE if path refers to an existing directory; otherwise, FALSE.
	 */
	public static function exists($filename) {
		return is_dir($filename);
	}
}
?>